#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年5月16日

@author: Administrator
'''
import logging
from comment import settings
from comment.models import Comment
from comment.tasks import update_comment_to_es_task

# 为loggers中定义的名称
logger = logging.getLogger("celery")

def update_comment_to_es(comments):
    '''
    function: 
    '''
    #判断comments 类型
    if isinstance(comments, Comment):
        comments = [comments]
    for comment in comments:
        #构造文章json字符串
        logger.info(u"start make comment es source by id %s " % comment.id) 
        _source = {
            "id":comment.id,
            "create_time" : comment.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "modify_time" : comment.modify_time.strftime("%Y-%m-%d %H:%M:%S"),
            "state" : comment.state,
            "state_desc" : settings.get_state_desc(comment.state),
            "content_type_id" : comment.content_type_id,
            "user_id" : comment.user_id,
            "user_name" : comment.user.first_name or comment.user.username,
            "object_id" : comment.object_id,
            "parent_id" : comment.parent_id if comment.parent else "",
            "content" : comment.content,
            "text_content" : comment.text_content,
            "floor" : comment.floor,
            "source" : comment.source,
            "source_desc" : settings.SOURCE_CHOICES[comment.source][1],
            "source_id" : comment.source_id,
            "like_count" : comment.like_count,
        }
        update_comment_to_es_task.delay(_source)
        logger.info(u"end make comment es source by id %s " % comment.id) 
